﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OAuth;
using OAuth.Sina;
using OAuth.NetEase;
using OAuth.Sohu;
using OAuth.QQ;

public partial class OAuthCallBack : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Type"] != null && Request.QueryString["Type"].ToString() != "")
            {
                switch (Request.QueryString["Type"])
                {
                    case "Sina": SinaCallBack(); break;
                    case "WangYi": WangYiCallBack(); break;
                    case "Sohu": SohuCallBack(); break;
                    case "QQ": QQCallBack(); break;
                    default: break;
                }
            }
            Page.ClientScript.RegisterStartupScript(GetType(), "JS", "<script language=\"javascript\" type=\"text/javascript\">CallBackSuccess()</script>");
        }
    }

    private void SinaCallBack()
    {
        SinaOAuthMode Mode = (SinaOAuthMode)Session["OAuthMode"];
        Mode.SetOAuthToken(new OAuthParameter("oauth_token", Request.QueryString["oauth_token"]));
        Mode.SetOAuthVerifier(new OAuthParameter("oauth_verifier", Request.QueryString["oauth_verifier"]));
        OAuthBase Base = new OAuthBase(new SinaOAuthRequestURL(), new SinaOAuthConfig(), Mode);
        Mode = (SinaOAuthMode)Base.RequestAccessToken();
        Session["OAuthMode"] = Mode;
    }

    private void WangYiCallBack()
    {
        NetEaseOAuthMode Mode = (NetEaseOAuthMode)Session["OAuthMode"];
        Mode.SetOAuthToken(new OAuthParameter("oauth_token", Request.QueryString["oauth_token"]));
        Mode.SetOAuthVerifier(new OAuthParameter("oauth_verifier", Request.QueryString["oauth_verifier"]));
        OAuthBase Base = new OAuthBase(new NetEaseOAuthRequestURL(), new NetEaseOAuthConfig(), Mode);
        Mode = (NetEaseOAuthMode)Base.RequestAccessToken();
        Session["OAuthMode"] = Mode;
    }

    private void SohuCallBack()
    {
        SohuOAuthMode Mode = (SohuOAuthMode)Session["OAuthMode"];
        Mode.SetOAuthToken(new OAuthParameter("oauth_token", Request.QueryString["oauth_token"]));
        Mode.SetOAuthVerifier(new OAuthParameter("oauth_verifier", Request.QueryString["oauth_verifier"]));
        OAuthBase Base = new OAuthBase(new SohuOAuthRequestURL(), new SohuOAuthConfig(), Mode);
        Mode = (SohuOAuthMode)Base.RequestAccessToken();
        Session["OAuthMode"] = Mode;
    }

    private void QQCallBack()
    {
        QQOAuthMode Mode = (QQOAuthMode)Session["OAuthMode"];
        Mode.SetOAuthToken(new OAuthParameter("oauth_token", Request.QueryString["oauth_token"]));
        Mode.SetOAuthVerifier(new OAuthParameter("oauth_verifier", Request.QueryString["oauth_verifier"]));
        OAuthBase Base = new OAuthBase(new QQOAuthRequestURL(), new QQOAuthConfig(), Mode);
        Mode = (QQOAuthMode)Base.RequestAccessToken();
        Session["OAuthMode"] = Mode;
    }
}